package com.example.loginpilot_01.controller;

import com.example.loginpilot_01.entity.Announcement;
import com.example.loginpilot_01.entity.FileInfo;
import com.example.loginpilot_01.service.announcement.AnnouncementService;
import com.example.loginpilot_01.service.file.FileService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Controller
@RequestMapping("/content") // 用户查看内容的前缀
public class ContentController {

    private final AnnouncementService announcementService;
    private final FileService fileService;

    // 构造方法注入
    public ContentController(AnnouncementService announcementService, FileService fileService) {
        this.announcementService = announcementService;
        this.fileService = fileService;
    }

    // 1. 公告列表页（所有登录用户可看）
    @GetMapping("/announcement/list")
    public String announcementList(Model model) {
        model.addAttribute("announcements", announcementService.getAllAnnouncements());
        return "content/announcement-list"; // 对应 templates/content/announcement-list.html
    }

    // 2. 公告详情页（查看单个公告）
    @GetMapping("/announcement/detail/{id}")
    public String announcementDetail(@PathVariable Integer id, Model model) {
        Announcement announcement = announcementService.getAnnouncementById(id);
        model.addAttribute("announcement", announcement);
        return "content/announcement-detail"; // 对应 templates/content/announcement-detail.html
    }

    // 3. 文件列表页（所有登录用户可看）
    @GetMapping("/file/list")
    public String fileList(Model model) {
        model.addAttribute("files", fileService.getAllFiles());
        return "content/file-list"; // 对应 templates/content/file-list.html
    }

    // 4. 文件下载（根据文件ID下载）
    @GetMapping("/file/download/{id}")
    public void downloadFile(@PathVariable Integer id, HttpServletResponse response) throws IOException {
        FileInfo fileInfo = fileService.getFileById(id);
        if (fileInfo == null) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND, "文件不存在");
            return;
        }

        // 拼接文件本地路径
        String uploadDir = System.getProperty("user.dir") + "/src/main/resources/static/upload/";
        String fileName = fileInfo.getFileUrl().substring("/upload/".length());
        Path filePath = Paths.get(uploadDir + fileName);

        // 设置响应头，强制浏览器使用自身下载器（而非调用第三方工具）
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileInfo.getFileName() + "\"; target=_blank");

        // 写入响应流，实现下载
        Files.copy(filePath, response.getOutputStream());
    }
}